#ifndef PROJECT_H
#define PROJECT_H

#include "stdbool.h"

#define COMMON_LIGHT_DARK   (4000)
#define COMMON_LIGHT_LIGHT  (100)
#define COMMON_LIGHT_VOL    (3.3f)
#define COMMON_DHT11_ERR_COUNT    (1 << 9) // dht11错误次数常量
#define COMMON_LIGHT_ERR_COUNT    (1 << 9) // dht11错误次数常量

#define COMMON_DHT11_HUMIDITY_CRITICAL (60) // 湿度临界值, 超过为潮湿
#define COMMON_DHT11_TEMPERATURE_CRITICAL (20) // 温度临界值, 超过为热
#define COMMON_LIGHT_PERCENT_CRITICAL (40) // 光照临界值, 超过为白天

#define COMMON_CONTROL_BACK_COUNT (3)
typedef struct {
    uint64_t equipId;
    bool run;
    bool autoControl;
    int controlBackCount;
    // c8t6 自行判断设备是否正常工作
    uint8_t dht11ErrCount;
    bool dht11NormalOper;
    uint8_t lightErrCount;
    bool lightNormalOper;
    /**
     * DHT11 START ************
     */
    bool dht11Suc;
    uint8_t dht11HumiInt;
    uint8_t dht11HumiDec;
    uint8_t dht11TempInt;
    uint8_t dht11TempDec;
    uint8_t dht11Check;
    float dht11Humidity;
    float dht11Temperature;
    /**
     * ************ DHT11 END
     */

    /**
     * LIGHT START ************
     */
    bool lightSuc;
    float lightPercent; // 光大值大
    uint16_t lightAdcVal; // 光大值小
    /**
     * ************ LIGHT END
     */

    /**
     * BYJ28 START ************
    */
    bool byj28State;
    /**
     * ************ BYJ28 END
     */


    /**
     * ESP01S START ************
     */
    bool esp01sState;
    /**
     * ************ ESP01S END
     */
} ProjectTypeDef;

void ProjectInit(ProjectTypeDef *project); // 工程初始化
void ProjectInitSensorParam(ProjectTypeDef *project); // 工程初始化传感器参数
int ProjectDataAssemble(const ProjectTypeDef *project, char *outputBuf, uint32_t size); // 工程数据组装
bool ProjectAutoControl(ProjectTypeDef *project);
void ProjectManualControl(ProjectTypeDef *project);
void ProjectLedTip(int c);

void Hardware_run(ProjectTypeDef *project);
void Hardware_stop(ProjectTypeDef *project);

void Hardware_motorRotateCW(void); // 步进电机顺转
void Hardware_motorRotateCCW(void); // 步进电机逆转
void Hardware_motorRotateCWRound(int round); // 步进电机顺转一圈
void Hardware_motorRotateCCWRound(int round); // 步进电机逆转一圈
void Hardware_motorOpen(ProjectTypeDef *project) ;//步进电机窗户开逻辑
void Hardware_motorClose(ProjectTypeDef *project);//步进电机窗户关逻辑


void Hardware_dht11Start(void); // dht11初始化
uint8_t Hardware_dht11CheckResponse(void); // 检查DHT11响应
uint8_t Hardware_dht11Read(void); // 读取一个bit
bool Hardware_dht11GetData(ProjectTypeDef *project); // 获取DHT11数据


void Hardware_lightInit(ADC_HandleTypeDef *hadc); // 光敏电阻初始化
uint16_t Hardware_lightReadOriginal(void); // 读取原始ADC值(0-4095)
float Hardware_lightReadVoltage(void); // 读取电压(0-3.3V)
float Hardware_lightReadPercent(ProjectTypeDef *project); // 读取光强(0-100%)


void Hardware_esp01sInit(void); //初始化
bool Hardware_esp01sTest(void); //AT测试
bool Hardware_esp01sConnectWiFi(void); //连接wifi
bool Hardware_esp01sSendHttpGet(const char *path, const char *data); // 发get
bool Hardware_esp01sSendHttpPost(const char *path, const char *data); // 发post
void Hardware_esp01sReset(void); // 重置
bool Hardware_esp01sWaitResponse(const char *expected, uint32_t timeout); // 等待返回数据
#endif //PROJECT_H
